<?php

namespace app\admin\model;

use app\model\Model;

class Addon extends Model
{
  public $privilege = 'addon';
  public $cids;
  public $id;
  public $db;
  public $res;

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->cids = checkAdminPriv(@$this->privilege); // 判断权限
    $this->id = intval($_GET['id']);
    $this->res = file_get('json', 'addon');
    $this->db = $GLOBALS['db'];
  }

  function index()
  {
    $this->tpl('addon', [
      'db' => $this->db,
      'res' => $this->res
    ]);
  }

  function add()
  {
    $this->res[] = get_post(['a_desc']);

    if (file_it(json_encode($this->res, 320), 'json', 'addon')) {
      admin_log('插件新增');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'addon'));
    }
    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'addon'));
  }

  function edit()
  {
    $this->tpl('addon_edit', [
      'db' => $this->db,
      'row' => $this->res[$this->id],
    ]);
  }

  function edit_action()
  {
    $this->res[$this->id] = get_post(['a_desc']);

    if (file_it(json_encode($this->res, 320), 'json', 'addon')) {
      admin_log('插件编辑 [' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'addon'));
    }

    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'addon'));
  }

  function del()
  {
    unset($this->res[$this->id]);

    if (file_it(json_encode($this->res, 320), 'json', 'addon')) {
      admin_log('插件删除 [' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'addon'));
    }

    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'addon'));
  }

  function __destruct()
  {
  }
}
